package com.ericsson.resourceplanner.service.impl;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import org.slf4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;

import com.ericsson.resourceplanner.dao.UserDao;
import com.ericsson.resourceplanner.dao.entity.Employee;
import com.ericsson.resourceplanner.dao.entity.EmployeeRole;
@Service
public class AppUserDetailsService implements UserDetailsService{
	
	protected final Logger logger = org.slf4j.LoggerFactory.getLogger(getClass());
	
	@Autowired
	private UserDao userDao;

	public UserDao getUserDao() {
		return userDao;
	}

	public void setUserDao(UserDao userDao) {
		this.userDao = userDao;
	}

	@Override
	public UserDetails loadUserByUsername(final String username)
			throws UsernameNotFoundException {
		// TODO Auto-generated method stub
		logger.info("loadUserByUsername username="+username);
		
		Employee employee= userDao.findByUserName(username);
		System.out.println("username in service"+employee.getEmpName());
		
		List<GrantedAuthority> authorities = buildUserAuthority(employee.getEmployeeRoles());
		 
		return buildUserForAuthentication(employee, authorities);
	}
	
	// Converts com.ericsson.resourceplanner.dao.entity.Employee user to
		// org.springframework.security.core.userdetails.User
	
	private User buildUserForAuthentication(Employee employee, 
			List<GrantedAuthority> authorities) {
			return new User(employee.getEmpName(), 
					employee.getPassword(), employee.isEnabled(), 
	                        true, true, true, authorities);
		}
	
	private List<GrantedAuthority> buildUserAuthority(List<EmployeeRole> userRoles) {
		 
		Set<GrantedAuthority> setAuths = new HashSet<GrantedAuthority>();
 
		// Build user's authorities
		for (EmployeeRole userRole : userRoles) {
			setAuths.add(new SimpleGrantedAuthority(userRole.getRole().getRoleName()));
			logger.info("loadUserByUsername userRoleName="+userRole.getRole().getRoleName());
		}
 
		List<GrantedAuthority> Result = new ArrayList<GrantedAuthority>(setAuths);
 
		return Result;
	}
	
	public void insertUserWithChangedPassword(String username,String pass)
	{
		//Employee employee= userDao.findByUserName(username);
		String passwordHashed = encryptPassword(pass);
		int checkSuccessfullUpdate = userDao.updateUserWithResettedPassword(username,passwordHashed);
	}

	private String encryptPassword(String password)
	{
		BCryptPasswordEncoder passwordEncoder = new BCryptPasswordEncoder();
		String hashedPassword = passwordEncoder.encode(password);
		return hashedPassword;
	}
	
}
